#include "EntityFreeList.h"

//Might inline it all into .h considering they aren't too large a methods

//Constructor takes in the amount of objects we want to pre-allocate memory for Game Entities
EntityFreeList::EntityFreeList(unsigned int startCount)
{
	for (unsigned int i = 0; i < startCount; ++i)
	{
		//Basically just allocate the space in memory for whenever we need this, we set the proper values for everything when we retrieve the object
		// ------ Modified by Monica so that it does not increment the number of entities in allEntities (needed for id of objects) ------ //
		 contents.push(new GameEntity(new SceneNode(), new PhysicsNode()));
		 // -----------------------------------------------------------------//
	}
}


EntityFreeList::~EntityFreeList()
{
	ClearList();
}

/* Method used to discard an object and return it to the freelist
 * Return: void
 * Parameters: Object to discard
 * Author: Borja
 */

void EntityFreeList::DiscardObject(GameEntity* object)
{
	contents.push(object); //add the returned entity to the back of the queue
}

/* Method used to retrieve a new object from the freelist
 * Expected Return: The front gameEntity of the queue (IE. The first available)
 * Parameters: void
 * Author: Borja
 */

GameEntity* EntityFreeList::GetNewObject()
{
	if (contents.empty()) //this is kinda the snag, we don't really want our list to be empty or else it'll have to create the entities on the fly -> kinda expensive
		return new GameEntity(new SceneNode(), new PhysicsNode());

	GameEntity* object = contents.front(); // we'll be returning the front of the queue
	contents.pop();//pop it from the queue
	return object; //return the object
}

/* Method used to clear the freelist and release the memory of it's contents
 * Return: void
 * Parameters: void
 * Author: Borja
 */

void EntityFreeList::ClearList()
{
	while (!contents.empty())
	{
		GameEntity* object = contents.front();
		contents.pop();
		//delete object;
	}
}